<?php


namespace app\api\model;

use think\Model;
use app\api\model\Wineshop as WM;
use app\api\model\Device as DM;

/*
 * 酒店
 * */
class Tavern extends Model
{

    // 表名
    protected $name = 'tavern';



    public function getTavern($params)
    {
        $where  = [];
        $lng    = $params['lng'];
        $lat    = $params['lat'];
        $page   = $params['page'] ?? 1;
        $rowL   = $params['row_list'] ?? 10;
 //        $list = self::where(['status' => 1])->order('weigh desc')->field('id,name,image')->select()->toArray();
        (!empty($params['name']))&& $where['name|address'] = ['like','%'.$params['name'].'%'];// 酒店名称
        (!empty($params['city']))&& $where['city'] = ['like','%'.$params['city'].'%'];//城市
        (!empty($params['tid'])) && $where['tid'] = ['=',$params['tid']];// 价格
        (!empty($params['qid'])) && $where['tid'] = ['=',$params['qid']];// 星级
        $order  = "distance ASC";
        (!empty($params['price'])) && $order    = "price desc";// 价格
        (!empty($params['distance'])) && $order = "distance desc";// 距离
        $list = self::field('id,name,image,label,price,sold,count,score,lat,lng,ST_DISTANCE_SPHERE(POINT(lng,lat), POINT('.$lng.', '.$lat.')) as distance')
            ->where($where)
            ->page($page,$rowL)
//            ->order('distance','ASC')
            ->order($order)
            ->select();
        if(!$list) return api_message();
        foreach ($list as $k => $val){
            $val->image     = full_url($val->image,true);
            $val->distance  = round($val->distance, 2);
//            $list[$k]['distance']   = round($val['distance'] / 1000, 2);
            $val->label     = explode(',',$val->label);
        }
        return api_message($list);
    }


    /*
     * 子酒店列表
     * */
    public function childTavern($params)
    {
        $page   = $params['page'] ?? 1;
        $rowL   = $params['row_list'] ?? 10;
        $lng    = $params['lng'];
        $lat    = $params['lat'];
        $field  = "id,name,ban,label,price,reservation,did,describe,address,lng,lat,policy,introduce,traffic,describe";
        $list   = self::field($field)->find($params['tid']);
        $dev    = DM::column('name,image','id');
        $div    = explode(',',$list->did);
        foreach($div as $vo)
        {
            $img = isset($dev[$vo]['image']) ? full_url($dev[$vo]['image']) : "";
            $nam = isset($dev[$vo]['name']) ? $dev[$vo]['name'] : "";
            $chi[] = ['img'=>$img,'name'=>$nam];
        }
        $list->did      = $chi;
        $list->dcount   = count($chi);
        $field  = "id,name,price,costprice,image,info,item";
        $child  = WM::where('tid',$params['tid'])->field($field)->select();
        foreach($child as $v)
        {
            $v->info    = json_decode(json_decode($v->info,true),true);
            $v->item    = json_decode(json_decode($v->item,true),true);
        }
        $list->ban  = $list->ban ? full_url($list->ban) : "";
        $where[]    = ['id', '<>', $params['tid'] ?? 0];
        $near       = self::field('id,name,image,label,price,sold,count,score,lat,lng,ST_DISTANCE_SPHERE(POINT(lng,lat), POINT('.$lng.', '.$lat.')) as distance')
            ->where($where)
            ->page($page,$rowL)
            ->order('distance','desc')
            ->select();
        foreach ($near as $k => $val){
            $val->image     = full_url($val->image,true);
            $val->distance  = round($val->distance, 2);
            $val->label     = explode(',',$val->label);
        }
        $res  = ['res'=>$list,'child'=>$child,'near'=>$near];
        return api_message($res);
    }


    /*
    * 详情
    * */
    public function desc($params)
    {
        $field      = "id,name,price,costprice,image,info";
        $res        = WM::field($field)->find($params['wid']);
        $res->info  = json_decode(json_decode($res->info,true),true);
        return api_message($res);
    }





}